package com.jic.member.controller.manage;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.constant.consist.ResponseCode;
import com.jic.member.entity.TUserGroup;
import com.jic.member.request.TUserGroupAddOrUpdateRequest;
import com.jic.member.request.TUserGroupRequest;
import com.jic.member.request.TripLabelArrayRequest;
import com.jic.member.request.TripUserGroupRequest;
import com.jic.member.response.TUserGroupResponse;
import com.jic.member.service.TUserGroupService;
import com.jic.member.service.TripBranchNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-06-04 15:47:07
 */
@RestController
@Slf4j
@RequestMapping("/tUserGroup")
@Api(tags = " [用户组表]控制类-PC")
public class TUserGroupController {
    @Autowired
    private TUserGroupService service;

    @Autowired
    private TripBranchNodeService tripBranchNodeService;

    /**
     * 新增记录
     *
     * @param request 新增[用户组表]请求参数
     * @return TUserGroupResponse 新增[用户组表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[用户组表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody TUserGroupRequest request) throws Throwable {
        //TODO 会保存记录
        TUserGroup tUserGroup = new TUserGroup();
        BeanUtils.copyProperties(request, tUserGroup);
        tUserGroup.setDeptId(request.getAdminOperate().getDeptId());
        tUserGroup.setDeptType(request.getAdminOperate().getDeptType().getCode());
        tUserGroup.setDeleteFlag(1);
        long flag = service.save(tUserGroup);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[用户组表]请求参数
     * @return TUserGroupResponse 新增或修改[用户组表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "更新[用户组表]")
    @PostMapping("/updateUserGroup")
    @ResponseBody
    public RestResult<String> updateUserGroup(@Validated @RequestBody TUserGroupAddOrUpdateRequest request) throws Throwable {
        return service.updateUserGroup(request);
    }

    /**
     * 删除记录
     *
     * @param request 删除[用户组表]请求参数
     * @return TUserGroupResponse 删除[用户组表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[用户组表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<String> delete(@RequestBody TUserGroupRequest request) throws Throwable {
        TUserGroup tUserGroup = new TUserGroup();
        BeanUtils.copyProperties(request, tUserGroup);
        return service.deleteById(tUserGroup);
    }

    /**
     * 修改记录
     *
     * @param request 修改[用户组表]请求参数
     * @return TUserGroupResponse 修改[用户组表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[用户组表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody TUserGroupRequest request) throws Throwable {
        TUserGroup tUserGroup = new TUserGroup();
        BeanUtils.copyProperties(request, tUserGroup);
        long flag = service.updateByPrimaryKeySelective(tUserGroup);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[用户组表]详情请求参数
     * @return TUserGroupResponse 查询[用户组表]详情响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[用户组表]详情")
    @ResponseBody
    public RestResult<TUserGroupResponse> getByPrimaryKey(@Validated @RequestBody TUserGroupRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            TUserGroup tUserGroup = new TUserGroup();
            BeanUtils.copyProperties(request, tUserGroup);
            TUserGroup result = service.getByPrimaryKey(tUserGroup);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            TUserGroupResponse bo = new TUserGroupResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new TUserGroupResponse());
    }

    /**
     * T_USER_GROUP 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [用户组表] ")
    @ResponseBody
    public RestResult<List<TUserGroupResponse>> listTUserGroupAllRecord(@RequestBody TUserGroupRequest request) throws Throwable {
        TUserGroup param = new TUserGroup();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        param.setDeptId(request.getAdminOperate().getDeptId());
        List<TUserGroup> list = service.listAllRecord(param);
        List<TUserGroupResponse> responseList = list.stream().map(temp -> {
            TUserGroupResponse bo = new TUserGroupResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * description:
     *
     * @author: jackHe
     * @time: 2020/6/9 13:55
     */
    @PostMapping("/queryPage")
    @ApiOperation(value = "分页查询[用户组表]")
    @ResponseBody
    public RestResult<PageResult<TUserGroupResponse>> queryPage(@RequestBody TUserGroupRequest request) {
        return service.queryPage(request);
    }

    @PostMapping("/queryMemberIds")
    @ApiOperation(value = "通过用户组和标签Ids查询会员")
    @ResponseBody
    public RestResult<List<String>> queryMemberIds(@RequestBody TripLabelArrayRequest tripLabelArrayRequest) {
        return RestResult.success(service.queryMemberIds(tripLabelArrayRequest));
    }

    @PostMapping("/refreshGroup")
    @ApiOperation(value = "刷新分组")
    @ResponseBody
    public RestResult<String> refreshGroup(@RequestBody TUserGroupAddOrUpdateRequest tUserGroupAddOrUpdateRequest) {
        return service.refreshGroup(tUserGroupAddOrUpdateRequest);
    }

    @PostMapping("/screeningMember")
    @ApiOperation(value = "筛选会员")
    @ResponseBody
    public RestResult<Integer> screeningMember(@RequestBody TUserGroupAddOrUpdateRequest tUserGroupAddOrUpdateRequest) {
        return RestResult.success(service.screeningMember(tUserGroupAddOrUpdateRequest).size());
    }

    @PostMapping("/updateLabelTableUserGroups")
    @ApiOperation(value = "用户组更新标签表")
    @ResponseBody
    public RestResult<String> updateLabelTableUserGroups(@RequestBody TUserGroupAddOrUpdateRequest tUserGroupAddOrUpdateRequest) {
        return service.updateLabelTableUserGroups(tUserGroupAddOrUpdateRequest);
    }

    @PostMapping("/checkUserGroup")
    @ApiOperation(value = "检查用户组状态")
    @ResponseBody
    public RestResult<Boolean> checkUserGroup(@RequestBody TripUserGroupRequest tripUserGroupRequest) {
        return RestResult.success(tripBranchNodeService.checkUserGroup(tripUserGroupRequest.getUserGroupId()));
    }
}

